package com.qxmly.fm.presenters;

import androidx.annotation.Nullable;

import com.qxmly.fm.interfaces.IDetailCallBack;
import com.qxmly.fm.interfaces.IDetailPresenter;
import com.qxmly.fm.utils.LogUtil;
import com.ximalaya.ting.android.opensdk.constants.DTransferConstants;
import com.ximalaya.ting.android.opensdk.datatrasfer.CommonRequest;
import com.ximalaya.ting.android.opensdk.datatrasfer.IDataCallBack;
import com.ximalaya.ting.android.opensdk.model.album.Album;
import com.ximalaya.ting.android.opensdk.model.track.Track;
import com.ximalaya.ting.android.opensdk.model.track.TrackList;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Q
 * Date:2021/11/29$
 **/
public class DetailPresenter implements IDetailPresenter {

    private final List<IDetailCallBack> mCallBack = new ArrayList<>();
    private static DetailPresenter sInstance;
    private Album mTargetAlbum = null;
    private final String TAG = "DetailPresenter";

    private DetailPresenter(){

    }

    public static DetailPresenter getInstance(){
        if (sInstance==null){
            synchronized (DetailPresenter.class){
                if(sInstance==null){
                    sInstance = new DetailPresenter();
                }
            }
        }
        return sInstance;
    }



    @Override
    public void pullRefreshMore() {

    }

    @Override
    public void loadMore() {

    }

    @Override
    public void getAlbumDetail(int albumId, int page) {
        //获取详情
        Map<String, String> map = new HashMap<String, String>();
        map.put(DTransferConstants.ALBUM_ID,albumId+"");
        map.put(DTransferConstants.SORT, "asc");
        map.put(DTransferConstants.PAGE, page+"");
        CommonRequest.getTracks(map, new IDataCallBack<TrackList>() {
            @Override
            public void onSuccess(@Nullable TrackList trackList) {
                if(trackList!=null){
                    List<Track> tracks = trackList.getTracks();
                   LogUtil.d(TAG,"getDetail success"+tracks.size());
                   handlerDetailResult(tracks);
                }

            }

            @Override
            public void onError(int errorCode, String errorMsg) {
                LogUtil.d(TAG,"error->"+errorCode);
                LogUtil.d(TAG,"error->"+errorMsg);

            }
        });

    }

    private void handlerDetailResult(List<Track> tracks) {
        for(IDetailCallBack mCallback : mCallBack){
            mCallback.onDetailListLoader(tracks);
        }
    }

    @Override
    public void registerCallBack(IDetailCallBack detailCallBack) {
        if(!mCallBack.contains(detailCallBack)){
            mCallBack.add(detailCallBack);
            if(mTargetAlbum!=null){
            detailCallBack.onAlbumLoader(mTargetAlbum);}
        }
    }

    @Override
    public void UnregisterCallBack(IDetailCallBack detailCallBack) {
        if(!mCallBack.contains(detailCallBack)){
            mCallBack.remove(detailCallBack);
        }

    }
    public void setTargetAlbum(Album targetAlbum){
        this.mTargetAlbum = targetAlbum;
    }


}
